﻿using System;

namespace Majoopic.Engine
{
    /// <summary>
    /// Message from the engine
    /// </summary>
    public class Message
    {
        #region Public Properties
        /// <summary>
        /// Message type
        /// </summary>
        public MessageType Type
        {
            get;
            set;
        }

        /// <summary>
        /// Inner exception
        /// </summary>
        public Exception InnerException 
        {
            get;
            set;
        }

        /// <summary>
        /// value of the message
        /// </summary>
        public string Value 
        {
            get;
            set;
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public Message() : base()
        {        
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Type of the message</param>
        /// <param name="message">Message</param>
        public Message(MessageType type, string message)
            : this(type, message, null)
        {
        }

        /// <summary>
        /// Constructor for an error message with an exception
        /// </summary>
        /// <param name="errorMessage">Error message</param>
        /// <param name="exception">Inner exception</param>
        public Message(string errorMessage, Exception exception)
            : this(MessageType.Error, errorMessage, exception)
        {            
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="messageType"></param>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public Message(MessageType messageType, string message, Exception exception)
            : this()
        {
            if (exception != null && messageType != MessageType.Error)
                messageType = MessageType.Error;
            this.Type = messageType;
            this.Value = message;
            this.InnerException = exception;
        }
        #endregion

        public override string ToString()
        {
            return this.Value;
        }
    }

    /// <summary>
    /// EventArgs to send a message to the listeners
    /// </summary>
    public class MessageEventArgs : EventArgs
    {
        #region Public Properties
        /// <summary>
        /// Message information
        /// </summary>
        public Message Message
        {
            get;
            set;
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public MessageEventArgs()
            : this(new Message())
        {

        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="message">Message</param>
        public MessageEventArgs(Message message)
            : base()
        {
            this.Message = message;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Type of the message</param>
        /// <param name="message">Message</param>
        public MessageEventArgs(MessageType type, string message)
            : this()
        {
            this.Message.Type = type;
            this.Message.Value = message;
        }

        /// <summary>
        /// Constructor for an error message with an exception
        /// </summary>
        /// <param name="errorMessage">Error message</param>
        /// <param name="exception">Inner exception</param>
        public MessageEventArgs(string errorMessage, Exception exception)
            : this(MessageType.Error, errorMessage)
        {
            this.Message.InnerException = exception;
        }
        #endregion
    }
}
